"use strict";
import util from './util.js'
import sys from './sys.js'
/*请求基本*/
export default {
	/*基本配置*/
	config: {
		url: '',
		data: {},
		method: "GET",
		dataType: "json",
		responseType: "text",
	},

	//基础请求方式
	request(options) {
		options = options || {};
		options.url = sys.getApiUrl() + options.url;
		options.header = {};
		options.contentType = options.contentType || 'form';
		if (options.contentType === "json") {
			options.header["content-type"] = "application/json;charset=UTF-8";
			options.header["Authorization"] = 'Bearer '+sys.getSysInfo().token;
		} else if (options.contentType == "form") {
			options.header["content-type"] = "application/x-www-form-urlencoded";
			options.header["Authorization"] = 'Bearer '+sys.getSysInfo().token;
		}
		delete options.contentType;
		return new Promise((resolve, reject) => {
			let _config = Object.assign({}, this.config, options);
			_config.requestId = new Date().getTime();
			_config.success = function(res) {
				if (res.data.code == 200) {
					resolve(res.data.data);
				} else {
					// util.showToast(res.data.data);
					reject(res.data.data);
				}
			};
			_config.fail = function(res) {
				let msg = res.errMsg || '';
				msg = msg.indexOf('request:fail') >= 0 ? "网络异常" : "未知错误";
				util.showToast(msg);
				reject(res);
			};
			uni.request(_config);
		});
	},
	
	//基础请求方式
	loginRequest(options) {
		options = options || {};
		options.url = sys.getApiUrl() + options.url;
		options.header = {};
		options.contentType = options.contentType || 'form';
		if (options.contentType === "json") {
			options.header["content-type"] = "application/json;charset=UTF-8";
		} else if (options.contentType == "form") {
			options.header["content-type"] = "application/x-www-form-urlencoded";
		}
		delete options.contentType;
		return new Promise((resolve, reject) => {
			let _config = Object.assign({}, this.config, options);
			_config.requestId = new Date().getTime();
			_config.success = function(res) {
				if (res.data.code == 200) {
					resolve(res.data.data);
				} else {
					// util.showToast(res.data.data);
					reject(res.data.data);
				}
			};
			_config.fail = function(res) {
				let msg = res.errMsg || '';
				msg = msg.indexOf('request:fail') >= 0 ? "网络异常" : "未知错误";
				util.showToast(msg);
				reject(res);
			};
			uni.request(_config);
		});
	},
	
	//8080基础请求方式
	ylRequest(options) {
		options = options || {};
		options.url = 'http://139.224.13.75/' + options.url;
		options.header = {};
		options.contentType = options.contentType || 'form';
		if (options.contentType === "jsonp") {
			options.header["content-type"] = "application/json;charset=UTF-8";
			options.header["Access-Control-Allow-Origin"] = '*';
		} else if (options.contentType == "form") {
			options.header["content-type"] = "application/x-www-form-urlencoded";
			options.header["Access-Control-Allow-Origin"] = '*';
		}
		delete options.contentType;
		return new Promise((resolve, reject) => {
			let _config = Object.assign({}, this.config, options);
			_config.requestId = new Date().getTime();
			_config.success = function(res) {
				if (res.data.code == 200) {
					resolve(res.data.data);
				} else {
					// util.showToast(res.data.data);
					reject(res.data.msg);
				}
			};
			_config.fail = function(res) {
				let msg = res.errMsg || '';
				msg = msg.indexOf('request:fail') >= 0 ? "网络异常" : "未知错误";
				util.showToast(msg);
				reject(res);
			};
			uni.request(_config);
		});
	},
	
	//后台网站接口基础请求方式
	requestData(options) {
		options = options || {};
		options.url = sys.getApiUrl() + options.url;
		options.header = {};
		options.contentType = options.contentType || 'form';
		if (options.contentType === "json") {
			options.header["content-type"] = "application/json;charset=UTF-8";
			options.header["Authorization"] = 'Bearer '+sys.getSysInfo().token;
		} else if (options.contentType == "form") {
			options.header["content-type"] = "application/json;charset=UTF-8";
			options.header["Authorization"] = 'Bearer '+sys.getSysInfo().token;
		}
		delete options.contentType;
		return new Promise((resolve, reject) => {
			let _config = Object.assign({}, this.config, options);
			_config.requestId = new Date().getTime();
			_config.success = function(res) {
				if (res.data.code == 200) {
					resolve(res.data.data);
				} else {
					// util.showToast(res.data.data);
					reject(res.data.msg);
				}
			};
			_config.fail = function(res) {
				let msg = res.errMsg || '';
				msg = msg.indexOf('request:fail') >= 0 ? "网络异常" : "未知错误";
				util.showToast(msg);
				reject(res);
			};
			uni.request(_config);
		});
	},

	/*
	 * get 请求
	 */
	get(url, data, options) {
		options = options || {};
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	
	/*
	 * 8080get 请求
	 */
	ylget(url, data, options) {
		options = options || {};
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.ylRequest(options)
	},
	/*
	 * post 请求
	 */
	post(url, data, options) {
		options = options || {};
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	
	/*
	 * loginPost 请求
	 */
	loginPost(url, data, options) {
		options = options || {};
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.loginRequest(options)
	},
	
	/*
	 * 后台网站post 请求
	 */
	postData(url, data, options) {
		options = options || {};
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.requestData(options)
	},
	/*
	 * put 请求
	 */
	put(url, data, options) {
		options = options || {};
		options.url = url
		options.data = data
		options.method = 'PUT'
		return this.request(options)
	},
	/*
	 * delete 请求
	 */
	delete(url, data, options) {
		options = options || {};
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options)
	}
};
